package com.cti.ftpservice.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

import com.cti.ftpservice.ftp.FTPDownFiles;
import com.cti.ftpservice.ftp.FTPUploadFiles;
import com.cti.ftpservice.ftp.FileUtils;
import com.cti.ftpservice.utils.FtpWsManagerUtil;
import com.ctibook.booksystem.ws.FtpFileUploadInfoViewVo;
/**
 * ftp服务器类
 * 
 * @author ljk
 *
 */
public class FtpServiceInfo {
	/**
	 * ftp类型   
	 * ftp_uploadFile：将其他ftp的文件推送到本ftp
	 * ftp_downloadFile:从本ftp下载文件
	 */
	public static final String FtpServiceType_UploadFile = "ftp_upload_file";
	public static final String FtpServiceType_DownloadFile = "ftp_download_file";
	private String id;
	/**
	 * 服务器名称
	 */
	private String ftpServiceName;
	/**
	 * ftp服务器类型 上传ftp or 下载ftp
	 */
	private String ftpServiceType;
	/**
	 * url(192.168.140.111)
	 */
	private String ftpURL;
	/**
	 * 用户名
	 */
	private String username;
	/**
	 * 密码
	 */
	private String pwd;
	/**
	 * 端口 2121
	 */
	private String ftpport;
	/**
	 * 上传数据路径
	 */
	private Queue<FtpFileUploadInfoViewVo> uploadFilePaths;
	/**
	 * 下载数据路径
	 */
	private Queue<FtpFileInfo> downFileInfos;
	
	/**
	 * 已经处理的书目
	 */
	private List<FtpFileInfo> dealFiles=new ArrayList<FtpFileInfo>();
	
    /**
     * 文件上传处理的记录
     */
	private List<FtpFileUploadInfoViewVo> uploadDealFiles=new ArrayList<FtpFileUploadInfoViewVo>();
	/**
	 * 文件保存路径
	 */
	private String savePath;
	/**
	 * 停止
	 */
	private boolean stop;
	/**
	 * 暂停
	 */
	private boolean pause;
	/**
	 * 线程数
	 */
	private int threadCount;

	/**
	 * 要下载书的总数
	 */
	private int bookTotalCount;

	/**
	 * 已经下载书的数量
	 */
	private int dealCount;

	/**
	 * 是否下载完成
	 */
	private boolean finished;

	public String getFtpURL() {
		return ftpURL;
	}

	public void setFtpURL(String ftpURL) {
		this.ftpURL = ftpURL;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getFtpport() {
		return ftpport;
	}

	public void setFtpport(String ftpport) {
		this.ftpport = ftpport;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public Queue<FtpFileUploadInfoViewVo> getUploadFilePaths() {
		return uploadFilePaths;
	}

	public void setUploadFilePaths(Queue<FtpFileUploadInfoViewVo> uploadFilePaths) {
		this.uploadFilePaths = uploadFilePaths;
	}

	public boolean getStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}

	public boolean getPause() {
		return pause;
	}

	public void setPause(boolean pause) {
		this.pause = pause;
	}

	public int getThreadCount() {
		return threadCount;
	}

	public void setThreadCount(int threadCount) {
		this.threadCount = threadCount;
	}

	public int getDealCount() {
		return dealCount;
	}

	public void setDealCount(int dealCount) {
		this.dealCount = dealCount;
	}

	public int getBookTotalCount() {
		return bookTotalCount;
	}

	public void setBookTotalCount(int bookTotalCount) {
		this.bookTotalCount = bookTotalCount;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	public boolean getFinished() {
		return finished;
	}

	
	public String getFtpServiceName() {
		return ftpServiceName;
	}

	public void setFtpServiceName(String ftpServiceName) {
		this.ftpServiceName = ftpServiceName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public Queue<FtpFileInfo> getDownFileInfos() {
		return downFileInfos;
	}

	public void setDownFileInfos(Queue<FtpFileInfo> downFileInfos) {
		this.downFileInfos = downFileInfos;
	}
	public List<FtpFileInfo> getDealFiles() {
		return dealFiles;
	}

	public void setDealFiles(List<FtpFileInfo> dealFiles) {
		this.dealFiles = dealFiles;
	}
	
	/**
	 * 下载文件完成
	 */
	public void stopDownLoadFiles() {
		try {
			synchronized (this) {
				setFinished(true);
				setStop(true);
			}
			FileUtils.saveDownLoadFileInfoToDB(id,dealFiles);
			dealFiles.clear();
		} catch (Exception e) {
			
		}
	}
	
	
	
	public void finish(){
		try {
			synchronized (this) {
				setFinished(true);
				setStop(true);
			}
			FileUtils.writeDownLoadFilePaths(id,dealFiles);
			dealFiles.clear();
		} catch (Exception e) {
			
		}
	}

	/**
	 * 暂停
	 */
	public void pause() {
		synchronized (this) {
			setPause(true);
		}
	}

	/**
	 * 恢复
	 */
	synchronized void resume() {
		synchronized (this) {
			setPause(false);
		}
		notify();
	}

	/**
	 * 开始
	 * @throws InterruptedException
	 * @throws NumberFormatException
	 * @throws IOException
	 */
	public void startDownLoad() throws InterruptedException, NumberFormatException,
			IOException {
		setStop(false);
		for (int i = 1; i <= threadCount; i++) {
			FTPDownFiles ftpDownFiles = new FTPDownFiles(this);
			Thread thread = new Thread(ftpDownFiles);
			thread.setName("downloadThread" + i);
			thread.start();
		}
	}

	
	public void startUpLoad(){
		setStop(false);
		for (int i = 1; i <= threadCount; i++) {
			FTPUploadFiles ftpUpFiles = new FTPUploadFiles(this);
			Thread thread = new Thread(ftpUpFiles);
			thread.setName("downloadThread" + i);
			thread.start();
		}
	}
	/**
	 * 获取下载列表中最顶端的元素
	 * 
	 * @return
	 */
	public FtpFileInfo getFirstDownloadFilePath() {
		synchronized (this) {
			if (downFileInfos == null || downFileInfos.isEmpty())
				return null;
			FtpFileInfo currentFileInfo = this.downFileInfos.poll();
			this.downFileInfos.remove(currentFileInfo);
			return currentFileInfo;
		}
	}
	/**
	 * 添加元素
	 * 
	 * @param downloadfilePath
	 */
	public void addDownloadFilePath(FtpFileInfo downloadfileInfo) {
		synchronized (this) {
			downFileInfos.offer(downloadfileInfo);
		}
	}

	
	public void adddealItem(FtpFileInfo dealInfo,boolean save) {
		synchronized (this) {
			if(save)
				dealFiles.add(dealInfo);
			dealCount++;
			if(dealFiles.size()>=300){
				clearDealFiles();
			}
		}
	}
	
	
	public void clearDealFiles(){
		FileUtils.saveDownLoadFileInfoToDB(id,dealFiles);
		dealFiles.clear();
	}
	
	
	/**
	 * 上传文件完成
	 */
	public void stopUpLoadFiles(){
		try {
			synchronized (this) {
				setFinished(true);
				setStop(true);
			}
			//保存数据库记录
			for (FtpFileUploadInfoViewVo ftpFileInfo : uploadDealFiles) {
				FtpWsManagerUtil.addUploadFtpFileRecord(ftpFileInfo,getId());
				String[] uploadrecordIds=FtpWsManagerUtil.getFtpFileMatchFtpServiceRecordIdsByFtpFileIdAndFtpServiceId(ftpFileInfo.getId(),getId());
				for (String uploadrecordId : uploadrecordIds) {
					FtpWsManagerUtil.updateFtpFileMatchFtpServiceRecordPassInfo(uploadrecordId,true);
				}
			}
			uploadDealFiles.clear();
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 获取上传列表中最顶端的元素
	 * 
	 * @return
	 */
	public FtpFileUploadInfoViewVo getFirstUpLoadFilePath() {
		synchronized (this) {
			if (uploadFilePaths == null || uploadFilePaths.isEmpty())
				return null;
			FtpFileUploadInfoViewVo currentFile = this.uploadFilePaths.poll();
			this.uploadFilePaths.remove(currentFile);
			return currentFile;
		}
	}
	
	/**
	 * 添加元素
	 * 
	 * @param downloadfilePath
	 */
	public void addUploadFilePath(FtpFileUploadInfoViewVo uploadfileInfo) {
		synchronized (this) {
			uploadFilePaths.offer(uploadfileInfo);
		}
	}

	
	public void adduploadDealItem(FtpFileUploadInfoViewVo dealInfo,boolean save) {
		synchronized (this) {
			if(save)
				uploadDealFiles.add(dealInfo);
			dealCount++;
		}
	}
	

	public String getFtpServiceType() {
		return ftpServiceType;
	}

	public void setFtpServiceType(String ftpServiceType) {
		this.ftpServiceType = ftpServiceType;
	}

	public List<FtpFileUploadInfoViewVo> getUploadDealFiles() {
		return uploadDealFiles;
	}

	public void setUploadDealFiles(List<FtpFileUploadInfoViewVo> uploadDealFiles) {
		this.uploadDealFiles = uploadDealFiles;
	}

}
